package com.naiyumie.ncalculator;

import java.math.BigDecimal;

import com.naiyumie.nlib.NWidgetTool;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 전체값의 몇 퍼센트는 얼마
 */
public class Percent02Activity extends KingWangJjangActivity {
	
	EditText eta1 = null, eta2 = null, eta3 = null;
	BigDecimal percentValue;
	String percentValueStr = null;
	BigDecimal totalValue;
	String totalValueStr = null;
	BigDecimal resultValue2;
	String resultValueStr2 = null;
	String msg = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_percent_02);
		Intent fromIntent = getIntent();
		msg = fromIntent.getStringExtra("MSG");
		eta1 = (EditText) findViewById(R.id.eta1);
		eta2 = (EditText) findViewById(R.id.eta2);
		eta3 = (EditText) findViewById(R.id.eta3);
		eta1.addTextChangedListener(wholeValuePercentWatcher);
		eta2.addTextChangedListener(wholeValuePercentWatcher);
		this.setMsg(msg);
		this.callMethod();
		tvTitle.setText(R.string.category02);
	}
	
	private final TextWatcher wholeValuePercentWatcher = new TextWatcher() {
		
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			eta3.setText("0");
		}
		
		public void onTextChanged(CharSequence s, int start, int before, int count) {
		}
		
		public void afterTextChanged(Editable s) {
			totalValueStr = nWidgetTool.stringFromEditText(eta2);
			totalValue = nWidgetTool.stringToBigDecimalFromEditText(eta2);
			percentValueStr = nWidgetTool.stringFromEditText(eta1);
			percentValue = nWidgetTool.stringToBigDecimalFromEditText(eta1);
			if (!percentValueStr.equals(null) && !percentValueStr.equals("") && !totalValueStr.equals(null) && !totalValueStr.equals("")) {
				BigDecimal calculate = totalValue.multiply(percentValue);
				BigDecimal calculateResult = calculate.divide(new BigDecimal(100), 4, BigDecimal.ROUND_HALF_UP);
				resultValueStr2 = String.valueOf(calculateResult);
				eta3.setText(resultValueStr2);
			}
		}
	};
}
